package com.kplan.join.action;

import javax.servlet.http.*;

import com.kplan.join.model.MemberDto;
import com.kplan.join.service.JoinService;
import com.kplan.util.StringCheck;

public class RegisterAction {
	public String action(HttpServletRequest request, HttpServletResponse response) {
	
	int rs = 0;
	MemberDto memberDto = new MemberDto();
	memberDto.setEmail(request.getParameter("email"));
	memberDto.setNick(request.getParameter("nick"));
	memberDto.setName(request.getParameter("name"));
	memberDto.setPass(request.getParameter("pass"));
	memberDto.setAge(StringCheck.nullToZero(request.getParameter("age")));
	memberDto.setGender(StringCheck.nullToZero(request.getParameter("gender")));
	memberDto.setTel1(request.getParameter("tel1"));
	memberDto.setTel2(request.getParameter("tel2"));
	memberDto.setTel3(request.getParameter("tel3"));
	String visited = "";
	String togo = "";
	String vs[] = request.getParameterValues("visited");
	String tg[] = request.getParameterValues("togo");
	String capta = (String) request.getSession().getAttribute("CAPTCHA");
	String captu = request.getParameter("captchaInput");
	
	if(vs != null) {
		int len = vs.length;
		for(int i=0;i<len;i++) {
			visited += vs[i] + " "; 
		}
	}
	if(tg != null) {
		int len = tg.length;
		for(int i=0;i<len;i++) {
			togo += tg[i] + " "; 
		}
	}
	memberDto.setVisited(visited);
	memberDto.setTogo(togo);
	
	if(capta.equals(captu)) {
		rs = JoinService.getInstance().register(memberDto);
	} else {
		rs = 9;
	}
	
	return rs == 1 ? "/join/joinsuccess.jsp" : "/join/joinfail.jsp?rs=" + rs;
	}
}
